package com.itljm.cxrMarket.entity.dto;

import com.itljm.cxrMarket.entity.Categories;
import com.itljm.cxrMarket.entity.Products;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ljm
 * @date 2024/12/31 15:16
 */
@Data
public class CategoryProductsDTO {
    private String categoryName;
    private Integer categoryId;
    private String bannerUrl;
    private List<Products> productsList;

    public CategoryProductsDTO(String categoryName, Integer categoryId,String bannerUrl) {
        this.categoryName = categoryName;
        this.categoryId = categoryId;
        this.bannerUrl = bannerUrl;
        this.productsList = new ArrayList<>();
    }
    public static List<CategoryProductsDTO> processProducts(List<Products> productsList, List<Categories> categories) {
        // 创建一个 map 来存储不同类别的产品
        Map<String, List<Products>> categoryMap = new HashMap<>();

        for (Products product : productsList) {
            String categoryName = product.getCategoryName();
            categoryMap.computeIfAbsent(categoryName, k -> new ArrayList<>()).add(product);
        }

        // 创建 CategoryProductsDTO 列表
        List<CategoryProductsDTO> categoryProductsDTOs = new ArrayList<>();
        for (Map.Entry<String, List<Products>> entry : categoryMap.entrySet()) {
            String categoryName = entry.getKey();

            for (Categories category : categories) {
                // 创建DTO对象，存放产品和类别信息
                CategoryProductsDTO dto = new CategoryProductsDTO(categoryName, category.getCategoryId(), category.getBannerUrl());
                dto.setProductsList(entry.getValue());

                categoryProductsDTOs.add(dto);
            }
        }

        return categoryProductsDTOs;
    }
}
